package com.wz.utils.concurrency.concurrenthashmap;

import java.util.concurrent.locks.ReentrantLock;

public class ReentrantLockTest {

    public static void main(String[] args) throws InterruptedException {
        final ReentrantLock reentrantLock = new ReentrantLock();


        new Thread(new Runnable() {
            @Override
            public void run() {
                reentrantLock.lock();
                System.out.println("线程1获取到了锁");
                try {
                    Thread.sleep(30*1000);
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
                reentrantLock.unlock();
            }
        }).start();

        Thread.sleep(1000);

        new Thread(new Runnable() {
            @Override
            public void run() {
//                reentrantLock.lock();
                while (!reentrantLock.tryLock()) {
                    System.out.println("做其他的事情"); //put，锁，

                    //
                    reentrantLock.lock();

                }
                System.out.println("线程2获取到了锁");
                reentrantLock.unlock();
            }
        }).start();
    }
}
